//
// Created by guo on 24-4-25.
//

#include "loader.h"
#include <boost/filesystem.hpp>

void sort_files(std::vector<std::string>& files){
    std::sort(files.begin(),files.end(),
              [](const std::string& l, const std::string r)->bool{
       size_t l_pos_slash = l.find_last_of('/');
       size_t l_pos_dot = l.find_last_of('.');
       std::string l_name = l.substr(l_pos_slash+1,l_pos_dot-l_pos_slash-1);

      size_t r_pos_slash = r.find_last_of('/');
      size_t r_pos_dot = r.find_last_of('.');
      std::string r_name = r.substr(r_pos_slash+1,r_pos_dot-r_pos_slash-1);

      return stoi(l_name, nullptr) < stod(r_name, nullptr);
    });
}

bool search_dir(const char *dir, std::vector<std::string> &vec_path) {
    boost::filesystem::path file_path = dir;
    boost::filesystem::directory_iterator dir_it(file_path);
    for(const auto& entry : dir_it){
        if(is_regular_file(entry)){
            vec_path.push_back(entry.path().string());
        }
    }

    sort_files(vec_path);
    return true;
}
